package vmcs.simulationsystem;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto
//

import java.awt.*;
import java.awt.event.*;

import vmcs.common.VMCSException;
import vmcs.store.Store;
import vmcs.store.StoreController;
import vmcs.store.StoreItem;
import vmcs.store.StoreObject;

public class StoreViewer extends Panel {

    private LabelledDisplay	viewItems[];
    private StoreController     storeCtrl;
    private int                type;
    public static final String        CASH_VIEW_TITLE = "Quantity of Coins Available";
    public static final String       DRINK_VIEW_TITLE = "Quantity of Drinks Available";
    private int                 sSize;  // store size;

    public StoreViewer (int ti, StoreController sctrl) {
        storeCtrl = sctrl;

        Label       lb;

        type = ti;
        if(type == Store.CASH){
          lb = new Label(CASH_VIEW_TITLE);
        }
        else {
          lb = new Label(DRINK_VIEW_TITLE);

        }
         lb.setFont(new Font ("Helvetica", Font.BOLD, 12));

        sSize = storeCtrl.getStoreSize(type);

        Panel   pl = new Panel(new FlowLayout(FlowLayout.LEFT));
        pl.add(lb);

        viewItems = new LabelledDisplay[sSize];

        StoreItem[]       storeItem;
        storeItem = storeCtrl.getStoreItems(type);

        this.setLayout (new GridLayout (0, 1));

        this.add(pl);

        String   name;
        String   sval;
        int      val;
        int      i;

        StoreViewerListener       svl;
        StoreObject               ob;

        for(i = 0; i < sSize; i++){
            ob = storeItem[i].getContent();
            name = ob.getName();
            val = storeItem[i].getQuantity();
            sval = String.valueOf(val);
            viewItems[i] = new LabelledDisplay(name, LabelledDisplay.DEFAULT, LabelledDisplay.GRID);
            viewItems[i].setValue(sval);
            svl = new StoreViewerListener(type, i, storeCtrl);
            viewItems[i].addListener(svl);
            this.add(viewItems[i]);
        }
    }


    public void update(int idx, int qty)
    throws VMCSException {
      if(idx >= sSize)
        throw new VMCSException ("StoreViewer.update", "index overflow");
      viewItems[idx].setValue(qty);
    }

    public void closeDown () {
    }

    public void setActive(boolean state){
      this.setEnabled(state);
    }
    
    public void update () {
        StoreItem[] storeItem = storeCtrl.getStoreItems(type);
        for (int i = 0; i < storeItem.length; i++) {
                int val = storeItem[i].getQuantity();
                String sval = String.valueOf(val);
                viewItems[i].setValue(sval);
        }
}
}

